package com.zte.cl.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.apache.log4j.Logger;

public class EHCacheUtil {

	private final static Logger log = Logger.getLogger(EHCacheUtil.class);

	public static CacheManager manager;

	static {
		try {
			manager = CacheManager.getInstance();
			if (manager == null)
				manager = CacheManager.create();
		} catch (CacheException e) {
			log.fatal("Initialize cache manager failed.", e);
		}
	}

	/**
	 * 从缓存中获取对象
	 * 
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public synchronized static Object getObjectCached(String cacheName, Object key) {
		Cache cache = getCache(cacheName);
		if (cache != null) {
			try {
				Element elem = cache.get(key);
				if (elem != null && !cache.isExpired(elem))
					return elem.getObjectValue();
			} catch (Exception e) {
				log.error("Get cache(" + cacheName + ") of " + key + " failed.", e);
			}
		}
		return null;
	}

	/**
	 * 把对象放入缓存中
	 * 
	 * @param cache_name
	 * @param key
	 * @param value
	 */
	public synchronized static void putObjectCached(String cacheName, Object key, Object value) {
		Cache cache = getCache(cacheName);
		if (cache != null) {
			try {
				cache.remove(key);
				Element elem = new Element(key, value);
				cache.put(elem);
			} catch (Exception e) {
				log.error("put cache(" + cacheName + ") of " + key + " failed.", e);
			}
		}
	}

	/**
	 * 获取指定名称的缓存
	 * 
	 * @param name
	 * @return
	 * @throws IllegalStateException
	 */
	public static Cache getCache(String name) throws IllegalStateException {
		return manager.getCache(name);
	}

	/**
	 * 获取缓冲中的信息
	 * 
	 * @param cache
	 * @param key
	 * @return
	 * @throws IllegalStateException
	 * @throws CacheException
	 */
	public static Element getElement(String cache, Object key) throws IllegalStateException, CacheException {
		Cache cCache = getCache(cache);
		return cCache.get(key);
	}

	/**
	 * 停止缓存管理器
	 */
	public static void shutdown() {
		if (manager != null)
			manager.shutdown();
	}

	/**
	 * 删除缓存
	 * @param cachename
	 * @date 2014-12-25
	 */
	public static void clearCache(String cachename) {
		try {
			manager.getCache(cachename).removeAll();
		} catch (IllegalStateException e) {
			log.error("clearCache cache(" + cachename + ") ", e);
		}
	}

	/**
	 * 删除指定缓存
	 * @param cachename
	 * @param key
	 * @date 2014-12-25
	 * @author Zjq
	 */
	public static void remove(String cachename, Object key) {
		manager.getCache(cachename).remove(key);
	}
	
	 /**
     * <b>function:</b> 返回具体的方法全路径名称 参数
     * @param targetName 全路径
     * @param methodName 方法名称
     * @param arguments 参数
     * @return 完整方法名称
     */
	public static String getCacheKey(String targetName, String methodName, Object[] arguments) {
        StringBuffer sb = new StringBuffer();
        sb.append(targetName).append(".").append(methodName);
        if ((arguments != null) && (arguments.length != 0)) {
            for (int i = 0; i < arguments.length; i++) {
                sb.append(".").append(arguments[i]);
            }
        }
        return sb.toString();
    }
}
